
const Controller = require('egg').Controller;

class VideoController extends Controller {

  // 获取book页面
  async getVideoList() {
    const videoList = await this.ctx.service.video.getVideoList();
    console.log(videoList);
    const { ctx } = this;
    await ctx.render("/pc/video.html",{
        videoList
    })
  }
  // 查询get请求
  async index() {
    try {
    let videoList = await this.ctx.service.video.getVideoList();
    this.ctx.body = {
      code:20000,
      message:true,
      data:videoList
    }
    } catch (error) {
      this.ctx.body = {
        code: 30000,
        message: false,
    }
    }
    
  }
  // 添加
  async create() {
    try {
        const body = this.ctx.request.body.form;
        console.log("@@@@@我是body",body);
        let videoList = await this.ctx.service.video.createVideo(body)   // 传递获取到的body给服务层的book.js的createBook方法
        this.ctx.body = {
            code: 20000,
            message: true,
            data:videoList
        }
    } catch (error) {
        this.ctx.body = {
            code: 30000,
            message: false,
        }
    }
}
  async update() {
      try {
        let id = this.ctx.params.id;
        let body = this.ctx.request.body.form;
        let update = await this.ctx.service.video.updateVideo(id,body)
        this.ctx.body = {
          code:20000,
          message:update
        }
      } catch (error) {
          this.ctx.body = {
            code:30000,
            message:false
          }
      }
  }
  async destroy() {
      try {
        let id = this.ctx.params.id;
        await this.ctx.service.video.deleteVideo(id)
        this.ctx.body = {
          code:20000,
          message:true
        }
      } catch (error) {
          this.ctx.body = {
            code:30000,
            message:false
          }
      }
  }

  async getVideoDetail(){
    let id = this.ctx.params.id;
    // await console.log("我是获取到的id",id);
    const videodetail = await this.ctx.service.video.getVideoDetail(id)
    await console.log("我是videodetail处的videodetail",videodetail);
    await this.ctx.render("/pc/video_detail.html",{videodetail})

  }
  
}

module.exports = VideoController;
